﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace LINQex
{
    class LinqExercise
    {
        static List<Student> CreateSampletudentList()
        {
            List<Student> list = new List<Student>()
            {
                new Student("Nikolay", "Kostov", "445606", "+359 2 5677832", "n.kostov@telerik.com", new List<int>(){4,5,6,3},5),
                new Student("Svetoslav", "Petsov", "44347", "08954334553", "s.petsov@telerik.com", new List<int>(){4,4,2,2},5),
                new Student("Ivan", "Dikov", "44457", "089581235653", "I.Dikov@telerik.com", new List<int>(){3,4},6),
                new Student("Svetlin", "Zamfirov", "44605", "089512475754", "s.zamf@abv.bg", new List<int>(){3,3,3,3},1),
                new Student("Delio", "Bozev", "44538", "02567892", "n.bozev@telerik.com", new List<int>(){4,5,2,2},2),
                new Student("Anna", "Delcheva", "44553", "0895865733", "a.delcheva@abv.bg", new List<int>(){3,3,3,3},3),
                new Student("Nikolay", "Kolev", "44555", "08958546783", "n.kolev@telerik.com", new List<int>(){6,6,5,6},2),
                new Student("Svetlin", "Nakov", "44557", "09898444534", "s.nakov@telerik.com", new List<int>(){4,4,5,6},5),
                new Student("Kristina", "Koeva", "44067", "025688945", "k.koeva@abv.bg", new List<int>(){5,5,5,3},1),
                new Student("Dimitar", "Petrov", "440706", "+3592645432", "d.petrov@telerik.com", new List<int>(){2,3,4,5,6},2),
                new Student("Doncho", "Minkov", "45677", "0895567223", "d.minkov@telerik.com", new List<int>(){4,5,6,3},2)
            };

            return list;
        }

        static void Main(string[] args)
        {
            List<Student> students = CreateSampletudentList();
            
            //ExtractSecondGroup(students);
            //ExtractStudentsWithAbvMail(students);
            //ExtractStudentsInSofia(students);
            //ExtractExcelentStudents(students);
            //ExtractWeakStudents(students);
            ExtractStudentsOf2006(students);

        }

        private static void ExtractStudentsWithAbvMail(IEnumerable<Student> students)
        {
            var studentsWithAbvMail =
                from student in students
                where student.Email.Contains("@abv.bg")
                select student;
            PrintStudents(studentsWithAbvMail);
        }

        private static void ExtractStudentsInSofia(IEnumerable<Student> students)
        {
            string pattern = @"(02|\+3592|\+359 2)([\s\d]*)";
            Regex template = new Regex(pattern);

            var studentsInSofia =
                from student in students
                where template.Matches(student.Phone).Count > 0
                select student;

            PrintStudents(studentsInSofia);
        }

        private static void ExtractSecondGroup(IEnumerable<Student> students)
        {
            IEnumerable<Student> secGropup =
                from student in students
                where student.GroupId == 2
                orderby student.FirstName
                select student;

            PrintStudents(secGropup);

            var secondGroup = students.Where(s => s.GroupId == 2).OrderBy(s => s.FirstName);
            PrintStudents(secondGroup);
        }

        private static void ExtractExcelentStudents(IEnumerable<Student> students)
        {
            var excellentStudents = students
                .Where(s => s.Marks.Contains(6))
                .Select(x => new { FullName = x.FirstName + " " + x.LastName, Marks = new List<int>(x.Marks) });

            foreach (var st in excellentStudents)
            {
                Console.Write("FullName :" + st.FullName + " Marks: ");
                foreach (int mark in st.Marks)
                {
                    Console.Write(mark + " ");
                }
                Console.WriteLine();
            }
        }

        private static void ExtractWeakStudents(IEnumerable<Student> students)
        {
            var weakStudents = students.Where(s => s.Marks.CountOf(2) == 2);

            PrintStudents(weakStudents);
        }

        private static void ExtractStudentsOf2006(IEnumerable<Student> students)
        {
            var year2006 =
                from student in students
                where student.Fn.Length >= 6 && student.Fn.Substring(4, 2) == "06"
                select student;

            PrintStudents(year2006);
        }

        private static void PrintStudents(IEnumerable<Student> secGropup)
        {
            foreach (var student in secGropup)
            {
                string studentStr = student.FirstName + " " + student.LastName +
                    " Fn" + student.Fn + " Email: " + student.Email +
                    " Group:" + student.GroupId + " Phone:" + student.Phone;

                Console.WriteLine(studentStr);
            }
            Console.WriteLine();
        }

    }

}
